﻿using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Action.Entity;
using T.FAS.Runtime.Action.SPI;

namespace T.FAS.Runtime.Action.Dao
{
    public class DataBaseInfoViewDao
    {
        public List<DataBaseInfoView> GetDataBaseInfoViews(List<DataBaseInfo> dbInfos)
        {
            var result = new List<DataBaseInfoView>();
            dbInfos.ForEach(dbInfo =>
            {
                var view = GetDataBaseInfoView(dbInfo);
                if (view!=null &&!result.Exists(x => x.Code.Equals(view.Code, StringComparison.OrdinalIgnoreCase)))
                    result.Add(view);
            });
            return result;
        }

        private DataBaseInfoView GetDataBaseInfoView(DataBaseInfo info)
        {
            try
            {
                return new DBInfoViewHelpFactory().GetDataBaseInfoViewByDataBaseCode(info);
            }
            catch (Exception ex)
            {
                return new DataBaseInfoView()
                {
                    ID = info.Code+info.DBType,
                    Code = info.Code,
                    Name = info.Code,
                    Description = $"{info.Code}数据库信息获取失败，请检查联调环境数据库配置信息。失败原因：{ex.ToString()}",
                };
            }
        }
    }
}
